/**
 *
 * @author John Creecy
 */

package battlebarnapplet;

import battlebarnapplet.entities.PlayerLocationEntity;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

import java.awt.Graphics;
import java.io.File;
import javax.imageio.ImageIO;

import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener {

	private static Board instance = null;
	private Timer timer;
	private Player player;
	private ImageIcon ii;
	private Image image;
	ImageIcon bgImage;
	private Sprite sprite;
	private Iterator it;
	public static int lastDirection;
	public static ConcurrentHashMap<String, PlayerLocationEntity> playerLocs = new ConcurrentHashMap<String, PlayerLocationEntity>();

	public Board() {
			addKeyListener(new TAdapter());
			setFocusable(true);
			//setBackground();
			setDoubleBuffered(true);
			player = new Player();
			ii = new ImageIcon(this.getClass().getResource("sprites/goat.gif"));
			bgImage = new ImageIcon(this.getClass().getResource("tiles/TileCanvas.png"));

			// redraw timer
			timer = new Timer(100, this);
			timer.start();
	}

	public static Board getInstance() {
		if (instance == null) {
			instance = new Board();
		}
		return instance;
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		image = bgImage.getImage();
		sprite = new Sprite(image);
		sprite.draw(g, 0, 0);
		image = ii.getImage();
		if (playerLocs.isEmpty()) {
			sprite = new Sprite(image);
			sprite.draw(g, 0, 0);
			sendDirection();
		} else {
			it = playerLocs.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry mapEntry = (Map.Entry) it.next();
				PlayerLocationEntity playerLocationEntity = (PlayerLocationEntity) mapEntry.getValue();
				sprite = new Sprite(image);
				sprite.draw(g, playerLocationEntity.getX(), playerLocationEntity.getY());
			}
		}
		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}

	public void actionPerformed(ActionEvent e) {
		repaint();
		if (lastDirection != Player.direction) {
			sendDirection();
		}
	}

	private void sendDirection() {

		try {
			DatagramSocket clientSocket = new DatagramSocket();
			InetAddress IPAddress = InetAddress.getByName("192.168.1.104");
			byte[] sendData = new byte[1024];
			sendData = Integer.toString(Player.direction).getBytes();
			DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9877);
			clientSocket.send(sendPacket);
			clientSocket.close();
			lastDirection = Player.direction;
		} catch (IOException ex) {
			Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	private class TAdapter extends KeyAdapter {

		public void keyReleased(KeyEvent e) {
			player.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {
			player.keyPressed(e);
		}
	}
}
